package com.testcolision.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.testcolision.OnscreenControlListener;
import com.testcolision.OnscreenControlRenderer;
import com.testcolision.Renderer;
import com.testcolision.simulation.TerrainSimulation;

public class GameScreen implements Screen {

	private final TerrainSimulation simulation;
	private final Renderer renderer;
	private OnscreenControlListener controlListener;
	private OnscreenControlRenderer controlRenderer;
 
	public GameScreen() {
		simulation = new TerrainSimulation();
		renderer = new Renderer();
		controlListener = new OnscreenControlListener();
		controlRenderer = new OnscreenControlRenderer();
	}

	public void update(float delta) {
		simulation.throttle = 0;
		simulation.steering = 0;
		simulation.brakes = 0;
			
		if ( Gdx.input.isKeyPressed(Keys.DPAD_LEFT))
			simulation.steering = -1;
		if (Gdx.input.isKeyPressed(Keys.DPAD_RIGHT))
			simulation.steering = 1;
		if (Gdx.input.isKeyPressed(Keys.DPAD_UP))
			simulation.throttle = 1;
		if (Gdx.input.isKeyPressed(Keys.DPAD_DOWN))
			simulation.throttle = -1;
		if ( Gdx.input.isKeyPressed(Keys.SPACE))
			simulation.brakes = 1;

		

		// Input listener multitouch
		for (int i = 0; i < 2; i++) {
			if (Gdx.input.isTouched(i)) {
				if (controlListener.leftButton(Gdx.input.getX(i), Gdx.input.getY(i))) {
					simulation.steering = -1;
				} else if (controlListener.rightButton(Gdx.input.getX(i), Gdx.input.getY(i))) {
					simulation.steering = 1;
				} else if (controlListener.upButton(Gdx.input.getX(i), Gdx.input.getY(i))) {
					simulation.throttle = 1;
				} else if (controlListener.controlButton(Gdx.input.getX(i), Gdx.input.getY(i))) {
					simulation.brakes = 1;
				}
			}
		}
		simulation.update(0.01f);
	}

	public void draw(float delta) {
		renderer.render(simulation, delta);
		controlRenderer.render();
	}

	@Override
	public void render(float delta) {
		update(delta);
		draw(delta);
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
